package com.dwei.entity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.dom4j.Element;

import com.dwei.Global;

public class Entity {
//============================================================================
//parameter
//============================================================================
	/** 实体类名（首字母大写） */
	private String nameEntity;
	/** Dao接口名（首字母大写） */
	private String nameDao;
	/** Service接口名（首字母大写） */
	private String nameService;
	/** Service实现类名（首字母大写） */
	private String nameServiceImp;
	/** Mapper配置文件名 */
	private String nameMapper;
	/** Injection配置文件名 */
	private String nameInjection;
	/** 表名 */
	private String table;
	/** 表主键 */
	private String key;
	/** 是否添加插入映射 */
	private boolean insert = false;
	/** 是否添加更新映射 */
	private boolean update = false;
	/** 是否添加删除映射 */
	private boolean delete = false;
	/** 表列与属性对象映射 */
	private Map<String, Property> colPropMap = new HashMap<String, Property>();
	/** 实体类包含的属性 */
	private List<Property> properties = new ArrayList<Property>();
	/** 需要导入的类 */
	private List<String> imports = new ArrayList<String>();
//============================================================================
//constructor
//============================================================================
	public Entity(Element element) {
		setNameEntity(element.attributeValue(Global.ENTITY_NAME));
		setTable(element.attributeValue(Global.ENTITY_TABLE));
		String keys = element.attributeValue(Global.ENTITY_KEY);
		//暂不支持复合主键
		setKey(keys.split(",")[0]);
		setInsert(element.attributeValue(Global.ENTITY_INSERT).equalsIgnoreCase(Global.BOOLEAN_TRUE));
		setUpdate(element.attributeValue(Global.ENTITY_UPDATE).equalsIgnoreCase(Global.BOOLEAN_TRUE));
		setDelete(element.attributeValue(Global.ENTITY_DELETE).equalsIgnoreCase(Global.BOOLEAN_TRUE));
		Iterator<Element> itrt = element.elementIterator(Global.ENTITY_PROP);
		Property prop;
		
		int index;
		while (itrt.hasNext()) {
			prop = new Property(itrt.next());
			if (prop.getJavaType().contains(".")) {
				imports.add(prop.getJavaType());
				index = prop.getJavaType().lastIndexOf(".");
				prop.setJavaType(prop.getJavaType().substring(index + 1));
			}
			colPropMap.put(prop.getColumn(), prop);
			properties.add(prop);
		}
	}
//============================================================================
//private
//============================================================================
//============================================================================
//override
//============================================================================
	@Override
	public String toString() {
		StringBuffer sb = new StringBuffer();
		sb.append("{基本信息:{类名:").append(getNameEntity())
		.append(",Dao接口名:").append(getNameDao())
		.append(",Service接口名:").append(getNameService())
		.append(",Service实现类名:").append(getNameServiceImp())
		.append(",Mapper配置文件名:").append(getNameMapper())
		.append(",Injection配置文件名:").append(getNameInjection())
		.append(",表名:").append(getTable())
		.append(",主键:").append(getKey())
		.append(",插入:").append(isInsert())
		.append(",更新:").append(isUpdate())
		.append(",删除:").append(isDelete())
		.append("}").append(",\n属性:[");
		Iterator<Property> itrt = properties.iterator();
		while (itrt.hasNext())
		{
			sb.append(itrt.next().toString());
			if (itrt.hasNext()) sb.append(",");
		}
		sb.append("]").append("}");
		return sb.toString();
	}
//============================================================================
//getter/setter
//============================================================================
	public String getNameEntity() {
		return nameEntity;
	}
	public void setNameEntity(String nameEntity) {
		this.nameEntity = StringUtils.capitalize(nameEntity);
		setNameDao("I" + getNameEntity() + "Dao");
		setNameService("I" + getNameEntity() + "Service");
		setNameServiceImp(getNameEntity() + "ServiceImp");
	}
	public String getNameDao() {
		return nameDao;
	}
	public void setNameDao(String nameDao) {
		this.nameDao = nameDao;
	}
	public String getNameService() {
		return nameService;
	}
	public void setNameService(String nameService) {
		this.nameService = nameService;
	}
	public String getNameServiceImp() {
		return nameServiceImp;
	}
	public void setNameServiceImp(String nameServiceImp) {
		this.nameServiceImp = nameServiceImp;
	}
	public String getNameMapper() {
		return nameMapper;
	}
	public void setNameMapper(String nameMapper) {
		this.nameMapper = nameMapper;
	}
	public String getNameInjection() {
		return nameInjection;
	}
	public void setNameInjection(String nameInjection) {
		this.nameInjection = nameInjection;
	}
	public String getTable() {
		return table;
	}
	public void setTable(String table) {
		this.table = table;
	}
	public String getKey() {
		return key;
	}
	public void setKey(String key) {
		this.key = key;
	}
	public boolean isInsert() {
		return insert;
	}
	public void setInsert(boolean insert) {
		this.insert = insert;
	}
	public boolean isUpdate() {
		return update;
	}
	public void setUpdate(boolean update) {
		this.update = update;
	}
	public boolean isDelete() {
		return delete;
	}
	public void setDelete(boolean delete) {
		this.delete = delete;
	}
	public Map<String, Property> getColPropMap() {
		return colPropMap;
	}
	public void setColPropMap(Map<String, Property> colPropMap) {
		this.colPropMap = colPropMap;
	}
	public List<Property> getProperties() {
		return properties;
	}
	public void setProperties(List<Property> properties) {
		this.properties = properties;
	}
	public List<String> getImports() {
		return imports;
	}
	public void setImports(List<String> imports) {
		this.imports = imports;
	}
}
